package local.bbs.common.util;

import local.bbs.common.command.PagingCommand;
import local.bbs.cont.command.BBSCommand;

public class PagingUtil {
	
	public static PagingCommand getPaging(BBSCommand bbsCommand) {
		int totalPage = ((bbsCommand.getTotalCount() - 1) / bbsCommand.getCountPerPage()) + 1;
		
		int totalBlock = (totalPage - 1) / bbsCommand.getCountPerBlock();
		int nowBlock = (bbsCommand.getNowPage() - 1) / bbsCommand.getCountPerBlock();
		
		int firstPage = nowBlock > 0 ? 1 : 0;
		int lastPage = totalPage;
		int startPage = nowBlock * bbsCommand.getCountPerBlock() + 1;
		int endPage = bbsCommand.getCountPerBlock() * (nowBlock + 1);
		if(endPage > totalPage) endPage = totalPage;
		int prevPage = startPage - 1;
		int nextPage = endPage + 1;
		
		PagingCommand paging = new PagingCommand();
		paging.setNowPage(bbsCommand.getNowPage());
		paging.setTotalPage(totalPage);
		paging.setTotalBlock(totalBlock);
		paging.setNowBlock(nowBlock);
		paging.setFirstPage(firstPage);
		paging.setLastPage(lastPage);
		paging.setStartPage(startPage);
		paging.setEndPage(endPage);
		paging.setPrevPage(prevPage);
		paging.setNextPage(nextPage);
		
		return paging;
	}
	
}
